<template>
	<view class="">
		<view class="bg-fff p20 ">
			<u-search v-model="keyword"></u-search>
		</view>
		<view class="p20">
			<u-empty v-if="!list.length" marginTop="100"></u-empty>
			<view class="mb20 flex" :class="{'opa7':item.is_set===0}" v-for="(item,index) in list" :key="index"
				@click="itemClick(item)">
				<view class="color-666 fs40 p20" v-if="item.is_set===1">
					<u-icon name="checkmark-circle-fill" v-if="item.check" :color="$zs"></u-icon>
					<u-icon name="checkmark-circle" v-else></u-icon>
				</view>
				<view class="flex1"> 
					<masterUser :item="item" />
				</view>
			</view>
		</view>
		<view class="h120"></view>
		<view class="pf bottom0 left0 bg-fff w-max">
			<view class="flex">
				<view class="flex1 p20" v-if="$params.type=='set'">
					<u-button type="warning" ripple @click="setLeader">设置管理员</u-button>
				</view>
				<view class="flex1 p20" v-if="$params.type=='del'">
					<u-button type="warning" ripple @click="removeMaster">移除</u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword: '',
				data: '',
				list: []
			};
		},
		watch: {
			keyword() {
				this.$u.debounce(this.getList, 800)
			}
		},
		onLoad() {
			this.getList()
		},
		onReachBottom() {
			this.getList('down')
		},
		methods: {
			itemClick(item) {
				if (this.$params.type == 'search') {
					this.inviteMaster(item)
				} else {
					if (item.is_set) {
						item.check = !item.check
					} else {
						// this.$msg('不能选择')
					}
				}
			},
			setLeader() {
				let master_ids = this.list.filter(e => e.check).map(e => e.master_id).join()
				// if (!master_ids) return this.$msg('请选择成员')
				this.$request('/master/agency/setLeader', {
					...this.$params,
					master_ids,
				}).then(res => {
					this.$msg(res.msg)
					this.$navBack(1,1000)
				})
			},
			removeMaster() {
				let master_ids = this.list.filter(e => e.check).map(e => e.master_id).join()
				// if (!master_ids) return this.$msg('请选择成员')
				this.$request('/master/agency/removeMaster', {
					...this.$params,
					master_ids,
				}).then(res => {
					this.$msg(res.msg)
					this.getList()
				})
			},
			async inviteMaster(item) {
				await this.$showModal('提示', '确认邀请' + item.name + '加入团队吗')
				this.$request('/master/agency/inviteMaster', {
					...this.$params,
					master_id: item.master_id || item.id,
				}).then(res => {
					this.$msg(res.msg)
				})
			},
			getList(e) {
				e == 'down' ? this.page++ : this.page = 1
				let params = {
					...this.$params,
					keyword: this.keyword,
					page: this.page,
				}
				let api = '/master/agency/agencyMaster'
				if (this.$params.type == 'search') {
					api = '/master/agency/searchMaster'
					if (!params.keyword) return
				}
				this.$request(api, params).then(res => {
					let list = res.data.data
					e == 'down' ? this.list.push(...list) : this.list = list
				})
			},
		}
	}
</script>

<style lang="scss">

</style>